﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtfprLibrary.Exceptions;

namespace UtfprLibrary.Configurations.DesignTime {
    /// <summary>
    /// TODO
    /// </summary>
    public static class ConfigurationManager {
        /// <exception cref="UtfprLibrary.Exceptions.UtfprConfigurationsException">Thrown when an exception occurs.</exception>
        public static UtfprSectionBase GetSection(string sectionName) {
            try {
                if (sectionName == Properties.Resources.Configurations_Const_Section) {
                    return new UtfprSection();
                }

                return null;
            } catch (Exception ex) {
                throw new UtfprConfigurationsException("Section '" + Properties.Resources.Configurations_Const_Section + "' not found.", ex);
            }
        }

        /// <summary>
        /// TODO
        /// Retorna as strings de conexão da seção "connectionStrings"
        /// </summary>
        /// <exception cref="UtfprLibrary.Exceptions.UtfprConfigurationsException">Thrown when an exception occurs.</exception>
        public static string[] ConnectionStrings {
            get {
                try {
                    var settings = new ConnectionStringSection();
                    var connStrList = new List<string>();
                    foreach (var connStr in settings.ConnectionStrings) {
                        connStrList.Add(buildConnectionString(connStr.ConnectionString));
                    }

                    return connStrList.ToArray();
                } catch (Exception ex) {
                    throw new UtfprConfigurationsException(Properties.Resources.Configurations_DesignTime_ConfigurationManager_ExMsg_ConnectionStrings, ex);
                }
            }
        }

        /// <summary>
        /// TODO
        /// Monta uma string de conexão (por enquanto apenas para o Oracle)
        /// <add 
        ///     name="AgroBrasil.Security.WindowsForms.Properties.Settings.edmSecurityConnectionString" 
        ///     connectionString="
        ///         metadata=res://*/EDMSecurity.csdl|res://*/EDMSecurity.ssdl|res://*/EDMSecurity.msl;
        ///         provider=Devart.Data.Oracle;
        ///         provider connection string=&quot;
        ///         User Id=sistema;
        ///         Password=karitec;
        ///         Server=ORCL;
        ///         Home=oradb10g_home1;
        ///         Persist Security Info=True&quot;" 
        ///         providerName="System.Data.EntityClient" />
        /// 
        /// </summary>
        /// <exception cref="System.ArgumentException">Thrown when an exception occurs.</exception>
        private static string buildConnectionString(string connectionString) {
            try {
                string[] parameters = connectionString.Split(';');

                return new System.Data.EntityClient.EntityConnectionStringBuilder {
                    Metadata = parameters[0].Split('=')[1],
                    Provider = parameters[1].Split('=')[1],
                    //aqui tem que fazer alguma coisa pra deixar genérico
                    ProviderConnectionString = new Devart.Data.Oracle.OracleConnectionStringBuilder {
                        Server = parameters[4].Split('=')[1],
                        Home = parameters[5].Split('=')[1],
                        PersistSecurityInfo = parameters[6].Contains("True"),
                        UserId = parameters[2].Split('=')[2],
                        Password = parameters[3].Split('=')[1]
                    }.ConnectionString
                }.ConnectionString;

                //return new System.Data.EntityClient.EntityConnectionStringBuilder {
                //    Metadata = "res://*/EDMSecurity.csdl|res://*/EDMSecurity.ssdl|res://*/EDMSecurity.msl",
                //    Provider = "Devart.Data.Oracle",
                //    ProviderConnectionString = new Devart.Data.Oracle.OracleConnectionStringBuilder {
                //        Server = "ORCL",
                //        Home = "oradb10g_home1",
                //        PersistSecurityInfo = true,
                //        UserId = "sistema",
                //        Password = "karitec",
                //    }.ConnectionString
                //}.ConnectionString;
            } catch (Exception ex) {                
                throw new ArgumentException("Could not build the connection string.", ex);
            }
        }
    }
}